#!/usr/bin/env python
# -*- coding: utf8 -*-
# title       :
# description :
# author      :'ShenMeng'
from PySide import QtGui, QtCore

def get_text_image(image_size=[200, 200],
                   text='sheen',
                   text_size=200,
                   background_color=(0,0,0),
                   text_color=(255,255,255),
                   bold=True,
                   mask=None):
    result_size = QtCore.QSize(image_size[0], image_size[1])
    result_background_color = QtGui.QColor(background_color[0], background_color[1], background_color[2], 255)
    result_text_color = QtGui.QColor(text_color[0], text_color[1], text_color[2], 255)

    result_image = QtGui.QImage(result_size, QtGui.QImage.Format_ARGB32)
    result_image.fill(result_background_color)

    # 为这个QImage构造一个QPainter
    painter = QtGui.QPainter(result_image)

    # 设置画刷的组合模式CompositionMode_SourceOut这个模式为目标图像在上
    # 改变组合模式和上面的填充方式可以画出透明的图片。
    painter.setCompositionMode(QtGui.QPainter.CompositionMode_Source)#CompositionMode_DestinationOver)

    # 改变画笔和字体
    pen = painter.pen()
    pen.setColor(result_text_color)
    font = painter.font()
    # 加粗
    font.setBold(bold)
    # 改变字体大小
    if text_size > image_size[1] or text_size <= 0:
        text_size = image_size[1]
    font.setPixelSize(text_size)

    painter.setPen(pen)
    painter.setFont(font)

    # 将text写在Image的中心
    painter.drawText(result_image.rect(), QtCore.Qt.AlignCenter, str(text))
    painter.end()

    pixmap = QtGui.QPixmap.fromImage(result_image)
    if mask:
        result_pixmap = add_image_mask(pixmap, mask)
    else:
        result_pixmap = pixmap
    # # 这个为图片的压缩度。0/100
    # n = 100
    # # 将画好的图片保存起来,可以将png改为其他格式
    # result_image.save("text.png","PNG",n)

    return result_pixmap

def add_image_mask(image, mask):
    size = image.size()

    result_image = QtGui.QImage(size, QtGui.QImage.Format_ARGB32_Premultiplied)
    mask_pixmap = QtGui.QPixmap(mask)

    painter = QtGui.QPainter(result_image)
    painter.setCompositionMode(QtGui.QPainter.CompositionMode_Source)
    painter.fillRect(result_image.rect(), QtCore.Qt.transparent)

    painter.setCompositionMode(QtGui.QPainter.CompositionMode_SourceOver)
    painter.drawPixmap(0, 0, mask_pixmap.scaled(size))

    painter.setCompositionMode(QtGui.QPainter.CompositionMode_SourceOut)
    painter.drawPixmap(0, 0, image.scaled(size))

    painter.setCompositionMode(QtGui.QPainter.CompositionMode_DestinationOver)
    painter.end()

    result_pixmap = QtGui.QPixmap.fromImage(result_image)
    return result_pixmap


def get_mask_pixmap(image, mask, size):
    source_image = QtGui.QPixmap(image)
    mask_image = QtGui.QPixmap(mask)
    result_size = QtCore.QSize(size[0], size[1])
    result_image = QtGui.QImage(result_size, QtGui.QImage.Format_ARGB32_Premultiplied)

    painter = QtGui.QPainter(result_image)
    painter.setCompositionMode(QtGui.QPainter.CompositionMode_Source)
    painter.fillRect(result_image.rect(), QtCore.Qt.transparent)

    painter.setCompositionMode(QtGui.QPainter.CompositionMode_SourceOver)
    painter.drawPixmap(0, 0, mask_image.scaled(result_size))

    painter.setCompositionMode(QtGui.QPainter.CompositionMode_SourceOut)
    painter.drawPixmap(0, 0, source_image.scaled(result_size))

    painter.setCompositionMode(QtGui.QPainter.CompositionMode_DestinationOver)
    painter.end()

    result_pixmap = QtGui.QPixmap.fromImage(result_image)
    return result_pixmap

def get_size_image(color, size):
    pix = QtGui.QPixmap(size[0], size[1])
    pix.fill(color)
    return pix